﻿using ConsoleApplication1.Models;
using Feature.YiGuo.Framework.Dapper;
using Feature.YiGuo.Framework.Domain;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
        }

        public static void Run()
        {
            Feature.YiGuo.Framework.Dapper.Configuration.Initialize(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

            Feature.YiGuo.Framework.Dapper.Configuration.SingleMaxInsertCount = 10;
            Feature.YiGuo.Framework.Dapper.Configuration.DbLog = (sql, param, time) =>
            {
                var content = string.Format("SQL：【{0}】\r\n参数：【{1}】\r\n耗时：{2}毫秒", sql, JsonConvert.SerializeObject(param),
                           time);
                Console.WriteLine(content);
                Console.WriteLine("===========================================");
                Console.WriteLine("");
            };

            Feature.YiGuo.Framework.Dapper.Configuration.BusinessLog = (businessLog) =>
            {
                Console.WriteLine("====================审计日志begin=======================");
                Console.WriteLine("TableName:{0}", businessLog.TableName);
                Console.WriteLine("ChangeType:{0}", businessLog.ChangeType);
                Console.WriteLine("Id:{0}", businessLog.BusinessId);
                Console.WriteLine("BusinessSql:{0}", businessLog.BusinessSql);
                Console.WriteLine("=====================审计日志end======================");
                Console.WriteLine("");
            };

            Feature.YiGuo.Framework.SqlBuilder.Configuration.Initialize(Assembly.GetExecutingAssembly());

            TestInsert();

            Console.ReadKey();
        }

        private static void TestPage()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            PageParam page = new PageParam()
            {
                PageIndex = 1,
                PageSize = 5
            };

            var result = baseRepository.GetPaging(page, null, item => new { item.Id, item.ComponentName, item.CreateTime });

            foreach (var item in result.Data)
            {
                Console.WriteLine("{0}", item.ComponentName);
            }
        }

        private static void TestSelect()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            var id = Guid.Parse("3ACD80C4-8E54-4155-9997-1EEC4F0E6C6F");
            var result = baseRepository.GetSingle(item => item.Id == id);
        }
        private static void TestInsert()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            var model = new ComponentBaseEntity()
            {
                ComponentName = "TestComponentName",
            };

            var result = baseRepository.Insert(model);

            Console.WriteLine("执行完成", result, model.Id);
        }

        private static void TestUpdate()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            var model = new ComponentBaseEntity()
            {
                Id = Guid.Parse("3ACD80C4-8E54-4155-9997-1EEC4F0E6C6F"),
                ComponentName = "TestComponentUpdate",
            };

            var result = baseRepository.Update(model, item => new { item.ComponentName });

            Console.WriteLine("执行完成");
        }

        private static void TestDelete()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            var model = new ComponentBaseEntity()
            {
                Id = Guid.Parse("3ACD80C4-8E54-4155-9997-1EEC4F0E6C6F")
            };

            var result = baseRepository.Delete(model);

            Console.WriteLine("执行完成");
        }

        private static void TestInsertBatch()
        {
            var baseRepository = new BaseRepository<ComponentBaseEntity>();

            var models = new List<ComponentBaseEntity>();
            for (int i = 0; i < 100001; i++)
            {
                var model = new ComponentBaseEntity()
                {
                    ComponentName = "TestComponentUpdate" + i,
                };
                models.Add(model);
            }

            Stopwatch sw = Stopwatch.StartNew();

            var result = baseRepository.Insert(models.ToArray());

            sw.Stop();

            Console.WriteLine("执行时间{0}毫秒", sw.ElapsedMilliseconds);
        }
    }
}
